//
//  BLShopCarViewController.m
//  blanket
//
//  Created by Mac on 2017/12/5.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLShopCarViewController.h"
#import "BLShopCarTableViewCell.h"
#import "BLShopCarModel.h"

#import "BLShopCarRootTableViewCell.h"
#import "BLShopCarHotleCell.h"
#import "BLShopCarPointTableViewCell.h"
#import "BLShopCarBuyTableViewCell.h"
#import "BLShopCarCustomCell.h"

#import "BLShopCarJieSuanViewController.h"
#define shopcallHotelTableViewCellReuserIndetifier @"shopcallHotelTableViewCellReuserIndetifier"
#define shopcallPointTableViewCellReuserIndetifier @"shopcallPointTableViewCellReuserIndetifier"
#define shopcallBuyTableViewCellReuserIndetifier @"shopcallBuyTableViewCellReuserIndetifier"
#define shopcallCustomTableViewCellReuserIndetifier @"shopcallCustomTableViewCellReuserIndetifier"

@interface BLShopCarViewController ()<STActionSheetDelegate>
@property(nonatomic, strong) STButton                     *allButton;//全选
@property(nonatomic, strong) STLabel                     *moneyLable;//金额
@property(nonatomic, strong) STButton                     *cofrimButton;//购买或者删除
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, assign) NSInteger                     page;
@property(nonatomic, strong) STNoresultView                     *noresultView;
@property(nonatomic, strong) NSMutableArray <BLShopCarModel*>                    *dataSouce;
@property(nonatomic, strong) BLShopCarModel                     *selectedModel;

//洗涤 到私人定制的actionSheet
@property(nonatomic, strong) STActionSheet                     *washAccetionSheet;
@property(nonatomic, strong) STActionSheet                     *customAccetionSheet;
@property(nonatomic, strong) STActionSheet                     *pointAccetionSheet;
@property(nonatomic, strong) STActionSheet                     *goodAccetionSheet;
@property(nonatomic, strong) STActionSheet                     *selfAccetionSheet;
@end

@implementation BLShopCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"购物车";
    [self configSubView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (ios11) {
        if ([self.tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
            [self.tableView performSelector:@selector(setContentInsetAdjustmentBehavior:) withObject:@(2)];
        }
    }
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self sendMyShopingListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = self.page  +1;
        [self sendMyShopingListRequest];
    }];
    
    self.noresultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 120, self.tableView.width, 300)
                                                        title:@"抱歉,暂无该类商品"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                     
                                                 }];
    self.noresultView.hidden = YES;
    [self.tableView addSubview:self.noresultView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navigationBar].bottom, UIScreenWidth, 44)
                                                         andTitle:@[@"布草定制",@"积分商城",@"集采商城",@"私人定制"] handle:^(STSegementButton *sender) {
                                                             [weakSelf.tableView.mj_header beginRefreshing];
                                                         }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleColor = KL_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    [self.view addSubview:self.segementButton];
    
    self.tableView.frame =  CGRectMake(0, self.segementButton.bottom, UIScreenWidth, UIScreenHeight - self.segementButton.bottom - [TMUtils tabbar].height );
    [self.tableView registerClass:[BLShopCarHotleCell class] forCellReuseIdentifier:shopcallHotelTableViewCellReuserIndetifier];
    
    [self.tableView registerClass:[BLShopCarPointTableViewCell class] forCellReuseIdentifier:shopcallPointTableViewCellReuserIndetifier];
    
    [self.tableView registerClass:[BLShopCarBuyTableViewCell class] forCellReuseIdentifier:shopcallBuyTableViewCellReuserIndetifier];
    
    [self.tableView registerClass:[BLShopCarCustomCell class] forCellReuseIdentifier:shopcallCustomTableViewCellReuserIndetifier];
    
    [self configBootomeView];
}
- (void)configBootomeView{
    UIView  * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, [TMUtils tabbar].top, UIScreenWidth, 49)];
    bootomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bootomView];
    [bootomView st_showTopShadow];
    [bootomView st_showBottomLine];
    __weak typeof(self) weakSelf =  self;
    self.allButton = [[STButton alloc] initWithFrame:CGRectMake(10, 0, 60, 49)
                                               title:@"全选"
                                          titleColor:KL_firstTextColor
                                           titleFont:14
                                        cornerRadius:0
                                     backgroundColor:nil
                                     backgroundImage:nil
                                               image:[UIImage imageNamed:@"icon_未选中"]];
    self.allButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [self.allButton setImage:[UIImage imageNamed:@"icon_选中"] forState:UIControlStateSelected];
    [self.allButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAllButton];
    }];
    // [bootomView addSubview:self.allButton];
    
    self.cofrimButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 120, 49)
                                                  title:@"结算"
                                             titleColor:[UIColor whiteColor]
                                              titleFont:15
                                           cornerRadius:0
                                        backgroundColor:KL_BlueBackGroundColor
                                        backgroundImage:nil
                                                  image:nil];
    self.cofrimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cofrimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedConfimButton];
    }];
    [self.cofrimButton setTitle:@"删除" forState:UIControlStateSelected];
    self.cofrimButton.right = UIScreenWidth;
    [bootomView addSubview:self.cofrimButton];
    
    self.moneyLable = [[STLabel alloc] initWithFrame:CGRectMake(self.allButton.right + 10, 0, self.cofrimButton.left - self.allButton.right - 20, 49)
                                                text:@"合计:$5698"
                                           textColor:KL_redColor
                                                font:13
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentRight];
    self.moneyLable.attributedText = [self.moneyLable.text st_convertAttributeStringWithKeyWord:@"合计:"
                                                                                     attributes:@{NSForegroundColorAttributeName:KL_firstTextColor}];
    [bootomView addSubview:self.moneyLable];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BLShopCarTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BLShopCarModel* model = self.dataSouce[indexPath.section];
    //1洗涤2定制3积分4采集5私人
    BLShopCarRootTableViewCell * cell;
    if ([model.type isEqualToString:@"2"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:shopcallHotelTableViewCellReuserIndetifier];
    }else if([model.type isEqualToString:@"3"]){
        cell = [tableView dequeueReusableCellWithIdentifier:shopcallPointTableViewCellReuserIndetifier];
    }else if([model.type isEqualToString:@"4"]){
        cell = [tableView dequeueReusableCellWithIdentifier:shopcallBuyTableViewCellReuserIndetifier];
    }else if([model.type isEqualToString:@"5"]){
        cell = [tableView dequeueReusableCellWithIdentifier:shopcallCustomTableViewCellReuserIndetifier];
    }
    cell.model = self.dataSouce[indexPath.section];
    __weak typeof(self) weakSelf =  self;
    __weak typeof(cell) weakCell =  cell;
    [cell.chosedButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedChosedButton:sender indexPath:indexPath cell:weakCell];
    }];
    [cell.addView setAutoAddViewNumChanged:^(NSInteger num) {
        
    }];
    [cell.addView setAutoAddViewEndEdit:^(NSInteger num) {
        [weakSelf onSlectedAddView:num indexPath:indexPath];
    }];
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self sendDelRequestWithIndexPath:indexPath];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Private Method
- (void)reloadData{
    
    self.noresultView.hidden = self.dataSouce.count;
    if (self.dataSouce.count >= 10) {
        self.tableView.mj_footer.hidden =NO;
    }else{
        self.tableView.mj_footer.hidden = YES;
    }
    [self cacluteCurrentAllMoney];
    [self.tableView reloadData];
}
- (void)cacluteCurrentAllMoney{
    CGFloat money = 0;
    CGFloat point = 0;
    for (BLShopCarModel * model in self.dataSouce) {
        if (model.isSlected) {
            money = money + model.price.floatValue * model.num.integerValue;
             point = point + model.score.floatValue * model.num.integerValue;
        }
    }

    self.moneyLable.text = [NSString stringWithFormat:@"合计:%0.2f",money];
    
    if ([self.dataSouce.firstObject.type isEqualToString:@"3"]) {
        self.moneyLable.text = [NSString stringWithFormat:@"合计:%0.2f积分",point];
    }
    self.moneyLable.attributedText = [self.moneyLable.text st_convertAttributeStringWithKeyWord:@"合计:" attributes:@{NSForegroundColorAttributeName:KL_firstTextColor}];
}
//现金
- (CGFloat)allSelectedMoney{
    CGFloat money = 0;
    for (BLShopCarModel * model in self.dataSouce) {
        if (model.isSlected) {
            money = money + model.price.floatValue * model.num.integerValue;
        }
    }
    return money;
}
//积分情况下 现金
- (CGFloat)allPointMoney{
    CGFloat money = 0;
    for (BLShopCarModel * model in self.dataSouce) {
        if (model.isSlected) {
            money = money + model.score_money.floatValue * model.num.integerValue;
        }
    }
    return money;
}
//积分情况下 积分
- (CGFloat)allPoint{
    CGFloat money = 0;
    for (BLShopCarModel * model in self.dataSouce) {
        if (model.isSlected) {
            money = money + model.make_score.floatValue * model.num.integerValue;
        }
    }
    return money;
}
//积分商城 的所有积分
- (CGFloat)allPointGood{
    CGFloat money = 0;
    for (BLShopCarModel * model in self.dataSouce) {
        if (model.isSlected) {
            money = money + model.score.floatValue * model.num.integerValue;
        }
    }
    return money;
}
//酒店定制 赠送盖币
- (CGFloat)allGrade{
    CGFloat money = 0;
    for (BLShopCarModel * model in self.dataSouce) {
        if (model.isSlected) {
            money = money + model.grade.floatValue * model.num.integerValue;
        }
    }
    return money;
}
//获取勾选array
- (NSArray<BLShopCarModel*>*)selectedArray{
    NSMutableArray * selectedArray = [NSMutableArray new];
    for (BLShopCarModel * model in self.dataSouce) {
        if (model.isSlected) {
            [selectedArray addObject:model];
        }
    }
    return selectedArray;
}
//判断展示何种actionSheet
- (void)judgeShowActionSheet{
    NSString * type = self.dataSouce.firstObject.type;
    if ([type isEqualToString:@"2"]) {
        [self configCustomGoodSelfCustomActionSheet];
    }
    if ([type isEqualToString:@"3"]) {
        //积分
        CGFloat point = [self allPointGood];
        NSString * pointdes = [NSString stringWithFormat:@"您将积分支付￥%0.2f 积分",point];
        self.pointAccetionSheet = [[STActionSheet alloc] initWithTitle:@"支付方式"
                                                          buttonTitles:@[pointdes]
                                                           cancelTitle:@"取消"
                                                              delegate:self];
        [self.pointAccetionSheet show];
    }
    if ([type isEqualToString:@"4"]) {
        [self configCustomGoodSelfCustomActionSheet];
    }
    if ([type isEqualToString:@"5"]) {
        [self configCustomGoodSelfCustomActionSheet];
    }
    return;
    //废弃
    switch (self.segementButton.cureentIndex + 1) {
        case 1:
        {
            //布草洗涤，可以余额支付
            CGFloat money = [self allSelectedMoney];
            NSString * title1 = [NSString stringWithFormat:@"盖币支付%0.2f元",money];
            //                NSString * title2 = [NSString stringWithFormat:@"微信支付%0.2f元",money];
            //                NSString * title3 = [NSString stringWithFormat:@"支付宝支付%0.2f元",money];
            self.washAccetionSheet = [[STActionSheet alloc] initWithTitle:@"支付方式"
                                                             buttonTitles:@[title1]
                                                              cancelTitle:@"取消"
                                                                 delegate:self];
            [self.washAccetionSheet show];
            
        }
            break;
        case 2:
        {
            [self configCustomGoodSelfCustomActionSheet];
            
        }
            break;
        case 3:
        {
            //积分
            CGFloat point = [self allPointGood];
            NSString * pointdes = [NSString stringWithFormat:@"您将积分支付￥%0.2f 积分",point];
            self.pointAccetionSheet = [[STActionSheet alloc] initWithTitle:@"支付方式"
                                                               buttonTitles:@[pointdes]
                                                                cancelTitle:@"取消"
                                                                   delegate:self];
            [self.pointAccetionSheet show];
        }
            break;
        case 4:
        {
             [self configCustomGoodSelfCustomActionSheet];
        }
            break;
        case 5:
        {
             [self configCustomGoodSelfCustomActionSheet];
        }
            break;
        default:
            break;
    }
    
}
// 配置 布草定制，采集商城，私人定制
- (void)configCustomGoodSelfCustomActionSheet{
    //布草定制，现金 / 现金 + 积分
    CGFloat money = [self allSelectedMoney];
    CGFloat pointmoney = [self allPointMoney];
    CGFloat point = [self allPoint];
    NSString * jifenzhifubao = [NSString stringWithFormat:@"您将支付宝支付￥%0.2f元 + %0.2f 积分",
                                pointmoney,
                                point];
    
    NSString * jifenwechat = [NSString stringWithFormat:@"您将微信支付￥%0.2f元 + %0.2f 积分",
                              pointmoney,
                              point];
    NSString * zhifubao = [NSString stringWithFormat:@"您将支付宝支付￥%0.2f 元",money];
    NSString * wechat = [NSString stringWithFormat:@"您将微信支付￥%0.2f 元",money];
    self.customAccetionSheet = [[STActionSheet alloc] initWithTitle:@"支付方式"
                                                       buttonTitles:@[jifenzhifubao,jifenwechat,zhifubao,wechat]
                                                        cancelTitle:@"取消"
                                                           delegate:self];
    [self.customAccetionSheet show];
}
#pragma mark --Action Method
- (void)onSlectedChosedButton:(UIButton*)sender indexPath:(NSIndexPath*)indexPath cell:(BLShopCarRootTableViewCell*)cell{
    BLShopCarModel *model = self.dataSouce[indexPath.section];
    model.isSlected = !model.isSlected;
    cell.chosedButton.selected = model.isSlected;
    [self cacluteCurrentAllMoney];
}
- (void)onSlectedAddView:(NSInteger)num indexPath:(NSIndexPath*)indexPath{
    
    BLShopCarModel *model = self.dataSouce[indexPath.section];
    if (model.num.integerValue != num) {
        [self sendChangeCarNumWithIndexPath:indexPath andNum:@(num).description];
    }
    model.num = @(num).description;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self cacluteCurrentAllMoney];
    
    // [self sendChangeCarNumWithIndexPath:indexPath andNum:@(num).description];
}
//废弃-全选所有
- (void)onSelectedAllButton{
    self.allButton.selected = !self.allButton.selected;
}
- (void)onSelectedConfimButton{
    if ([self selectedArray].count == 0) {
        [SVProgressHUD showErrorWithStatus:@"您还未选择商品"];
        return;
    }
    BLShopCarJieSuanViewController * vc = [BLShopCarJieSuanViewController new];
    vc.dataSouce = [self selectedArray];
    vc.allMoneyDesc = self.moneyLable.text;
    [self.navigationController pushViewController:vc animated:YES];
   // [self judgeShowActionSheet];
}
//除了pay_type
- (NSMutableDictionary*)fetchCurrentDic{
    //    token     是     string     验证客户端是否合法的token     5fb0b61af324881e956b0e674376f97b
    //    uid     是     int     用户id
    //    card_id     是     int     购物车id     —多个用逗号拼接 如 1,2,3
    //    goods_intro     是     string     商品     —传json 列如：[{“gid”:”1”,”num”:”3”,”price”:”20”},{“gid”:”3”,”num”:”1”,”price”:”30”}]
    //    goods_type     是     int     类型     商品类型 1布草洗涤，2布草定制，3积分商城，4集采商城,5私人定制
    //    pay_type     是     int     支付方式     1,余额支付，2盖币加积分支付
    
    NSMutableDictionary  * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    NSMutableArray * cards = [NSMutableArray new];
    NSMutableArray * jsonArry = [NSMutableArray new];
    for (BLShopCarModel * model in [self selectedArray]) {
        [cards addObject:model.shopCarID];
        NSMutableDictionary * jsonDic = [NSMutableDictionary new];
        [jsonDic setObject:model.gid forKey:@"gid"];
        [jsonDic setObject:model.num forKey:@"num"];
        [jsonDic setObject:model.price forKey:@"price"];
        if (model.score.length) {
            [jsonDic setObject:model.score forKey:@"score"];
        }
        if (self.segementButton.cureentIndex == 1) {
             [jsonDic setObject:model.make_score forKey:@"score"];
        }
        [jsonArry addObject:jsonDic];
    }
    NSString * card_id = [cards componentsJoinedByString:@","];
    if (card_id.length) {
        [dic setObject:card_id forKey:@"card_id"];
    }
    NSString * jsonString = jsonArry.mj_JSONString;
    [dic setObject:jsonString forKey:@"goods_intro"];
    NSInteger num = self.segementButton.cureentIndex  +1;
    [dic setObject:@(num) forKey:@"goods_type"];
    return dic;
    
}
#pragma mark --STActionSheetDeLlegate
- (void)actionSheet:(STActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (actionSheet.titleArray.count <= buttonIndex) {
        //点击取消
        return;
    }
    //洗涤
    if (actionSheet == self.washAccetionSheet) {
        NSMutableDictionary * dic = [self fetchCurrentDic];
         [dic setObject:@"1" forKey:@"goods_type"];
        NSString * payType;
        //目前洗涤只能余额支付
        if (buttonIndex == 0) {
            //余额
            payType = @"4";
        }
        [dic setObject:payType forKey:@"pay_type"];
        [self fetchMutipleGoddsConfimRequestWithDic:dic];
    }
    //布草酒店定制，采集商城，和私人布草定制
    if (actionSheet == self.customAccetionSheet) {
        NSMutableDictionary * dic = [self fetchCurrentDic];
        BLShopCarModel * model = self.dataSouce.firstObject;
        [dic setObject:model.type forKey:@"goods_type"];
        NSString * payType;
        //定制
        if (buttonIndex == 0) {
            //积分 现金
            payType = @"1";
            [dic setObject:@"alipay" forKey:@"custompay_type"];
            CGFloat  price = [self allPointMoney];
            [dic setObject:@(price) forKey:@"price"];
            CGFloat  scroe = [self allPoint];
             [dic setObject:@(scroe) forKey:@"score"];
             [dic setObject:@(0) forKey:@"grade"];
        }
        if (buttonIndex == 1) {
            //微信
            payType = @"1";
             [dic setObject:@"wechat" forKey:@"custompay_type"];
            CGFloat  price = [self allPointMoney];
            [dic setObject:@(price) forKey:@"price"];
            CGFloat  scroe = [self allPoint];
            [dic setObject:@(scroe) forKey:@"score"];
            [dic setObject:@(0) forKey:@"grade"];
        }
        if (buttonIndex == 2) {
            //支付宝
            payType = @"2";
             [dic setObject:@"no" forKey:@"custompay_type"];
            CGFloat  price = [self allSelectedMoney];
            [dic setObject:@(price) forKey:@"price"];
            [dic setObject:@(0) forKey:@"score"];
            CGFloat  grade = [self allGrade];
            [dic setObject:@(grade) forKey:@"grade"];
        }
        if (buttonIndex == 3) {
            //wexin
            payType = @"3";
            [dic setObject:@"no" forKey:@"custompay_type"];
            CGFloat  price = [self allSelectedMoney];
            [dic setObject:@(price) forKey:@"price"];
            [dic setObject:@(0) forKey:@"score"];
            CGFloat  grade = [self allGrade];
            [dic setObject:@(grade) forKey:@"grade"];
        }
        if (buttonIndex == 4) {
            return;
        }
        [dic setObject:payType forKey:@"pay_type"];
        [self fetchHotleCustomGoddsConfimRequestWithDic:dic];
    }
    
    if (actionSheet == self.pointAccetionSheet) {
         NSMutableDictionary * dic = [self fetchCurrentDic];
        [dic setObject:@"3" forKey:@"goods_type"];
         [dic setObject:@"1" forKey:@"pay_type"];
        [self fetchPointCustomGoddsConfimRequestWithDic:dic];
    }
}
#pragma mark --NetWork Method
//新版多商品结算
- (void)fetchMutipleGoddsConfimRequestWithDic:(NSDictionary*)dic{
    
    
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/multiShoppingCardAction"];
    NSString * type = dic[@"pay_type"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSString * order =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 if ([type isEqualToString:@"1"]) {
                                                     //盖币加积分
                                                 }else if([type isEqualToString:@"2"]){
                                                     //支付宝
                                                     [TMUtils sendAliPayWithOrder:order];
                                                     
                                                 }else if([type isEqualToString:@"3"]){
                                                     //微信
                                                     [TMUtils sendWechatPayWithOrder:order];
                                                     
                                                 }else if([type isEqualToString:@"4"]){
                                                     //余额
                                                     [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                                                     [self.tableView.mj_header beginRefreshing];
                                                 }
                                                 
                                                 [self.navigationController popToRootViewControllerAnimated:YES];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//布草定制 私人定制 采集商城结算
- (void)fetchHotleCustomGoddsConfimRequestWithDic:(NSDictionary*)dic{
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/multiShoppingCardAction"];
    NSString * type = dic[@"pay_type"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSString * order =  responseObject[@"data"];
                                                 //积分支付时候 的支付宝和微信选择
                                                 if ([dic[@"custompay_type"] isEqualToString:@"alipay"]) {
                                                      [TMUtils sendAliPayWithOrder:order];
                                                     return ;
                                                 }
                                                 if ([dic[@"custompay_type"] isEqualToString:@"wechat"]) {
                                                     [TMUtils sendWechatPayWithOrder:order];
                                                      return ;
                                                 }
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 if ([type isEqualToString:@"1"]) {
                                                
                                                 }else if([type isEqualToString:@"2"]){
                                                     
                                                      [TMUtils sendAliPayWithOrder:order];
                                                     
                                                 }else if([type isEqualToString:@"3"]){
                                                     //支付宝
                                                      [TMUtils sendWechatPayWithOrder:order];
                                                     
                                                 }
                                                 
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//积分结算
- (void)fetchPointCustomGoddsConfimRequestWithDic:(NSDictionary*)dic{
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/multiShoppingCardAction"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_header beginRefreshing];
                                                 [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendMyShopingListRequest{
    NSInteger num = self.segementButton.cureentIndex +2;
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/userCardAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [dic setObject:@(num) forKey:@"type"];
    [dic setObject:@(self.page) forKey:@"page"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * dataArray =  responseObject[@"data"];
                                                 NSLog(@"json = %@",dataArray.mj_JSONString);
                                                 if (self.page == 1) {
                                                     self.dataSouce = [NSMutableArray new];
                                                     self.selectedModel = nil;
                                                 }
                                                 for (NSDictionary * modelDic in dataArray) {
                                                     BLShopCarModel * model = [BLShopCarModel mj_objectWithKeyValues:modelDic];
                                                     NSArray * goods = modelDic[@"goods"];
                                                     model.cap_coin = [goods.firstObject valueForKey:@"cap_coin"];
                                                     [self.dataSouce addObject:model];
                                                     
                                                 }
                                                 
                                                 
                                                 [self reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                                 [SVProgressHUD dismiss];
                                                 if ([error.desc isEqualToString:@"没有更多数据"]
                                                     && self.page != 1) {
                                                     return ;
                                                 }
                                                 self.dataSouce = [NSMutableArray new];
                                                 [self reloadData];
                                                 
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//废弃
- (void)sendCommitRequeestWithPayType:(NSString*)type{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/createOrderAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    
    [dic setObject:self.selectedModel.gid forKey:@"gid"];
    [dic setObject:self.selectedModel.price forKey:@"price"];
    
    [dic setObject:self.selectedModel.num forKey:@"num"];
    
    [dic setObject:self.selectedModel.type forKey:@"goods_type"];
    
    [dic setObject:@"2" forKey:@"display"];
    
    [dic setObject:type forKey:@"type"];
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSString * order =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 if ([type isEqualToString:@"1"]) {
                                                     [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                                                     [self.tableView.mj_header beginRefreshing];
                                                 }
                                                 if ([type isEqualToString:@"3"]) {
                                                     //微信
                                                     [TMUtils sendWechatPayWithOrder:order];
                                                     
                                                 }else if([type isEqualToString:@"2"]){
                                                     //支付宝
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

//删除购物车
- (void)sendDelRequestWithIndexPath:(NSIndexPath*)indexPath{
    [SVProgressHUD showWithStatus:@"请稍后"];
    BLShopCarModel * model = self.dataSouce[indexPath.section];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/delShoppingCard"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (model.shopCarID) {
        [dic setObject:model.shopCarID forKey:@"cid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                                                 [self.tableView.mj_header beginRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//修改数量
- (void)sendChangeCarNumWithIndexPath:(NSIndexPath*)indexPath andNum:(NSString*)num{
    [SVProgressHUD showWithStatus:@"请稍后"];
    BLShopCarModel * model = self.dataSouce[indexPath.section];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/editCardAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (model.shopCarID) {
        [dic setObject:model.shopCarID forKey:@"cid"];
    }
    if (num.description) {
        [dic setObject:num forKey:@"num"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"修改数量成功"];
                                                 // [self.tableView.mj_header beginRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

//废弃
- (void)sendByWithRequestWithType:(NSInteger)type{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/buyGatherAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (self.selectedModel.gid.length) {
        [dic setObject:self.selectedModel.gid forKey:@"gid"];
    }
    [dic setObject:self.selectedModel.num forKey:@"num"];
    
    
    [dic setObject:@(self.segementButton.cureentIndex + 1) forKey:@"goods_type"];
    
    NSDictionary * test1 = @{@"gid":@"1",@"num":@"3",@"price":@"20"};
    NSDictionary * test2 = @{@"gid":@"3",@"num":@"1",@"price":@"30"};
    NSArray * testArry = @[test1,test2];
    NSString * json = testArry.mj_JSONString;
    [dic setObject:json forKey:@"jsonTest"];
    
    [dic setObject:@(type) forKey:@"pay_type"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"结算成功"];
                                                 [self.tableView.mj_header beginRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

